/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wiiscript;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Evan
 */
public class Properties {
    private static String[] properties;
    public static void init() {

        URL fileURL = ClassLoader.getSystemResource("res/properties.txt");
        try {
            ArrayList<String> propList = new ArrayList<String>();
            BufferedReader reader = new BufferedReader(new FileReader(fileURL.getPath()));
            for(String s : getProps(reader)) {
                propList.add(s);
            }
            reader.close();

            properties = propList.toArray(new String[propList.size()]);
        } catch (IOException ex) {
            Logger.getLogger(Properties.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private static ArrayList<String> getProps(BufferedReader reader) throws IOException {
        ArrayList<String> propList = new ArrayList<String>();
        String line;
        while((line = reader.readLine()) != null && !line.contains("}")) {
            if(line.contains("{")) {
                String front = line.replace("{", "").trim();
                for(String prop : getProps(reader)) {
                    propList.add(front + "." + prop);
                }
            } else {
                propList.add(line.trim());
            }
        }
        return propList;
    }
    public static String[] getProperties() {
        return properties;
    }

    public static String[] getProperties(String device) {
        ArrayList<String> props = new ArrayList<String>();
        for(String s : properties) {
            if(s.contains(device)) {
                String prop = s.replace(device + ".", "");
                if(!props.contains(prop)) {
                    props.add(prop);
                }
            }
        }
        return props.toArray(new String[props.size()]);
    }
}
